<?php

	function escape( $string ) {
		if( ( get_magic_quotes_runtime() == 1 ) || ( get_magic_quotes_gpc() == 1 ) ) {
			$string = stripslashes( $string );
		}
		return mysql_real_escape_string( $string );
	}

	function getOption( $option ) {
		try {
			return 'package';
			$options = getMultiple( 'option', array( 'optionName||' . $option ) );
			return $options->getResultCount() > 0 ? $options->getOptionValue() : '';
		} catch( Exception $ex ) {
			return 'default';
		}
	}

	function isAjaxRequest() {
		return isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] );
	}

	function sanitizeWithDashes( $title ) {
		$title = strtolower( strip_tags( $title ) );
		foreach( array( '/&.+?;/', '/[^A-Za-z0-9 _-]/', '/\s+/', '|-+|' ) as $regex ) {
			$title = preg_replace( $regex, '-', $title );
		}
		return trim( $title, '-' );
	}

	function camelToUnderScore( $string ) {
		$newStr = '';
		for( $i = 0; $i < strlen( $string ); $i++ ) {
			$char = substr( $string, $i, 1 );
			if( strtolower( $char ) != $char ) {
				$newStr .= '_' . strtolower( $char );
			} else {
				$newStr .= $char;
			}
		}
		return $newStr;
	}

	function describe() {
		$arguments = func_get_args();
		if( count( $arguments ) == 0 ) {
			describe( 'Exit Call Handled . . .' );
			exit;
		}
		foreach( $arguments as $index => $thisVariable ) {
			if( $index == count( $arguments ) - 1 && count( $arguments ) != 1 && is_bool( $thisVariable ) ) {
				break;
			}
			if( is_string( $thisVariable ) ) {
				echo "\r\n<pre>" . $thisVariable . '</pre>';
			} else if( is_bool( $thisVariable ) ) {
				echo "\r\n<pre>";
				var_dump( $thisVariable );
				echo '</pre>';
			} else {
				echo "\r\n<pre>";
				print_r( $thisVariable );
				echo '</pre>';
			}
			flush();
		}
		if( $arguments[ count( $arguments ) - 1 ] === true && count( $arguments ) != 1 ) {
			describe( 'Exit Call Handled . . .' );
			exit;
		}
	}

	function getDb( $table, $task = 'show' ) {
		$className = ucfirst( MYSQL_PREFIX ) . 'Db_'. ucfirst( $table );
		if( !class_exists( $className ) ) {
			Loader::generateDbClass( $className );
		}
		return new $className( $task );
	}
	function getSingle( $table, $id ) {
		$className = ucfirst( MYSQL_PREFIX ) . ucfirst( $table );
		if( !class_exists( $className ) ) {
			Loader::generateSingularClass( $className );
		}
		return new $className( $id );
	}
	function getMultiple( $table, $filter = array() ) {
		$className = ucfirst( MYSQL_PREFIX ) . ucfirst( $table ) . 's';
		if( !class_exists( $className ) ) {
			Loader::generatePluralClass( $className );
		}
		if( is_string( $filter ) ) {
			$filter = func_get_args();
			unset( $filter[ 0 ] );
		}
		return new $className( $filter );
	}